<?php

namespace PhpYes\Com;


final class RedisConfig
{
    /** @var string */
    private $name;
    /** @var array */
    private $config;

    /** @var string */
    private $host;
    /** @var int */
    private $port;
    /** @var float */
    private $timeout;
    /** @var null|string */
    private $password;

    /**
     * RedisConfig constructor.
     * @param string $name
     * @param array $config
     */
    public function __construct($name, $config)
    {
        $this->name = $name;
        $this->config = $config;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return array
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @return string
     */
    public function getHost()
    {
        if (empty($this->host)) {
            $this->host = empty($this->config['host']) ? null : trim($this->config['host']);
        }

        return $this->host;
    }

    /**
     * @return int
     */
    public function getPort()
    {
        if (empty($this->port)) {
            $this->port = empty($this->config['port']) ? 6379 : intval($this->config['port']);
        }

        return $this->port;
    }

    /**
     * @return float
     */
    public function getTimeout()
    {
        if (empty($this->timeout)) {
            $this->timeout = empty($this->config['timeout']) ? 0.0 : floatval($this->config['timeout']);
        }

        return $this->timeout;
    }

    /**
     * @return null|string
     */
    public function getPassword()
    {
        if (empty($this->password)) {
            $this->password = empty($this->config['password']) ? null : trim($this->config['password']);
        }

        return $this->password;
    }
}